package com.showde.shoppingmall.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kim.androidbase.tools.StringUtils;
import com.showde.shoppingmall.R;
import com.showde.shoppingmall.ShoppingMallApplication;
import com.showde.shoppingmall.UserSession;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.LoginLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.UserDBCommand;
import com.showde.shoppingmall.domain.User;
import com.showde.shoppingmall.utils.ActivityDispatch;
import com.showde.shoppingmall.utils.ToastUtils;
import com.showde.shoppingmall.view.ProgressDialog;

public class LoginActivity extends BaseActivity {

	private static final String TAG = "LoginActivity";
	private static final int MSG_LOGIN = 0x001;
	private static final int MSG_LOGIN_FAIL = 0x002;
	private static final int MSG_EXIT = 0x003;

	private EditText userAccountEditText;
	private EditText passwordEditText;

	private TextView loginTextView;
	private TextView registerTextView;
	private TextView forgetpasswordTextView;

	private UserSession userSession;
	private User user;
	private UserDBCommand userDBCommand;

	private String userAccount;
	private String password;

	private boolean loginSuccess = false;

	ProgressDialog dialog;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_LOGIN:
				ToastUtils.showToast(LoginActivity.this, "登录成功...");
				dismissDialog();
				handler.sendEmptyMessageDelayed(MSG_EXIT, 100);
				break;
			case MSG_LOGIN_FAIL:
				dismissDialog();
				ToastUtils.showToast(LoginActivity.this, "登录失败...");
				break;
			case MSG_EXIT:
				ActivityDispatch.backToAboveActivity(ActivityDispatch.getActivityGroup(groupTag));
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txt_login:
			login();
			break;
		case R.id.txt_register:
			toRegister();
			break;
		case R.id.txt_forgetpassword:
			break;
		}
	}

	@Override
	protected void initParam() {
		if (userSession == null) {
			userSession = ShoppingMallApplication.userSession;
		}
		if (userDBCommand == null) {
			userDBCommand = new UserDBCommand(this);
		}
		if (userSession == null || userSession.getSessionUser() == null) {
			user = userDBCommand.queryCurrentUser();
			if (user != null) {
				userSession = new UserSession();
				userSession.setSession(user);
			}
		} else {
			if ((user = userSession.getSessionUser()) == null) {
				user = userDBCommand.queryCurrentUser();
				userSession.setSession(user);
			}
		}
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_login);
		userAccountEditText = (EditText) findViewById(R.id.edt_useraccount);
		passwordEditText = (EditText) findViewById(R.id.edt_password);

		loginTextView = (TextView) findViewById(R.id.txt_login);
		registerTextView = (TextView) findViewById(R.id.txt_register);
		forgetpasswordTextView = (TextView) findViewById(R.id.txt_forgetpassword);

		loginTextView.setOnClickListener(this);
		registerTextView.setOnClickListener(this);
		forgetpasswordTextView.setOnClickListener(this);

		if (user != null) {
			userAccountEditText.setText(user.getUserAccount());
			passwordEditText.setText(user.getUserPassword());
		}

	}

	private boolean prepare() {
		userAccount = userAccountEditText.getText().toString();
		password = passwordEditText.getText().toString();

		if (StringUtils.isEmptyOrNull(userAccount)
				|| StringUtils.isEmptyOrNull(password)) {
			ToastUtils.showToast(this, R.string.msg_account_not_complete);
			return false;
		}
		return true;
	}

	private void login() {
		if (!prepare()) {
			return;
		}

		if (dialog != null) {
			dialog.dismiss();
		}
		dialog = new ProgressDialog(this.getParent(), "正在登录...",
				R.layout.view_progress);
		dialog.show();

		LoginLogic.login(this, userAccount, password,
				new UICallbackInterface() {

					@Override
					public void onSuccess(CallbackData data) {
						data = null;
						user = ShoppingMallApplication.userSession
								.getSessionUser();
						if (user != null) {
							loginSuccess = true;
							handler.sendEmptyMessage(MSG_LOGIN);
						}else{
							handler.sendEmptyMessage(MSG_LOGIN_FAIL);
						}
					}

					@Override
					public void onError(String errorMsg) {
						// TODO
						handler.sendEmptyMessage(MSG_LOGIN_FAIL);
					}
				});
	}

	private void toRegister() {
		ActivityDispatch.toRegister(this);
	}

	private void dismissDialog() {
		if (dialog != null) {
			dialog.dismiss();
		}
	}

}
